#include <all_wlib.h>
#pragma hdrstop

#include "int.h"

void __fastcall LoadSquid( HWND Handle, HOperation& rop, PRPeriod& h, CONSTSTR nm, AddUrlItem_p cb )
  {  char     str[ 5*1024 ], *m;
     FILE    *f = fopen( nm, "r" );
     UrlInfo  it;
     __int64  cn = 0;

     if ( !f ) {
       AskMessage( Handle, NULL, "Error opening source file:\n\"%s\"\nError: %s", nm, FIO_ERROR );
       return;
     }

     bool working = true;
     long len = filelength( fileno(f) );

     rop.SetAbort(true);
     while( working && fgets( str, sizeof(str), f ) && !feof(f) ) {
       ProcessMessages();

       //Check abort
       if ( rop.isAborted() &&
            AskYesNo( Handle, NULL, "Terminate loading?" ) ) {
         return;
       }

       //Refresh info
       if ( h.End() ) {
         rop.SetProgress( 100. * ftell(f) / len );
         rop.SetText( "Loading [%s]...\n\nLoaded: %I64u items", nm, cn );
       }

       DWORD tm, ms;                       // 1127216103.051
       DWORD time;                         // 0
       char  laddr[ 20 ],                  // 194.226.246.43
             type[ 50 ];                   // TCP_DENIED/403
       int   size;                         // 1128
       char  method[ 20 ];                 // GET
       char  path[ 1024 ];                 // http://www123073.adnet.ru/cgi-bin/iframe/mp3real?
       char  rfc[ 20 ];                    // -
       char  direct[ 50 ];                 // NONE/-

       sscanf( str, "%d.%d %d %s %s %d %s %s %s %s",
               &tm, &ms, &time, &laddr, &type, &size, &method, &path, &rfc, &direct );

       if ( strncmp( direct, "NONE/", 5 ) == 0 )
         continue;

       if ( strncmp( direct, "DIRECT/", 7 ) == 0 ) {
         m = strchr( path,'/' );
         if ( m ) {
           if ( m[1] == '/' )
             m = strchr( m+2,'/' );
         }

         char *tmp = strchr( m ? m : path,':' );
         if ( tmp ) *tmp = 0;

         if ( m ) {
           it.Url = m+1;
           *m = 0;
           it.Site = path;
         } else {
           it.Site = path;
           it.Url  = "";
         }

         it.Size = size;
         it.Time.Set( (time_t)tm );
         it.Host = laddr;

         cn++;
         if ( !cb( it ) )
           return;
         continue;
       }

       //?? other types
     }
     fclose(f);
}